import javax.swing.tree.TreeNode;

/*
 * @lc app=leetcode.cn id=101 lang=java
 *
 * [101] 对称二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        //递归写法
        return comapre(root.left, root.right);
        
    }
    public boolean comapre(TreeNode left,TreeNode right){
        //终止条件
        if(left==null&&right!=null){
            return false;
        }else if(left!=null&&right==null){
            return false;
        }else if(left==null&&right==null){
            return true;
        }else if(left.val!=right.val){
            return false;
        }
        //递归操作
        boolean outside=comapre(left.left, right.right);
        boolean inside=comapre(left.right, right.left);
        return outside&&inside;
    }

    
}

//熟练后写法
// class Solution {
//     public boolean isSymmetric(TreeNode root) {
//         return isMirror(root.left,root.right);
//     }

//     public boolean isMirror(TreeNode root1,TreeNode root2){
//         if(root1==null&&root2==null) return true;//特殊情况，都为null为true
//         if(root1==null||root2==null) return false;//只有一方为null，为false
//         return root1.val==root2.val&&isMirror(root1.left,root2.right)&&isMirror(root1.right,root2.left);
//     }
// }
// @lc code=end

